import pygame, sys, math, random
from pygame.locals import *
if not pygame.font: print 'Warning, fonts disabled'
if not pygame.mixer: print 'Warning, sound disabled'
from Player import Player
from Shot import Shot
from IceShot import IceShot
from FireShot import FireShot
from WaterShot import WaterShot
from LightningShot import LightningShot
from Block import Block
from Spikes import Spikes
from Enemy import Enemy
from FireWarrior import FireWarrior
from BigBird import BigBird
from DemonBunny import DemonBunny
from Background import Background
from HardBlock import HardBlock


clock = pygame.time.Clock()

width = 800
height = 600
screensize = width, height

blocksize = [50,50]
playersize = [40,40]

screen = pygame.display.set_mode(screensize)
bgColor = r,g,b = 0,0,0

shots = pygame.sprite.Group()
spikes = pygame.sprite.Group()
bigbirds = pygame.sprite.Group()
demonbunnies = pygame.sprite.Group()
firewarriors = pygame.sprite.Group()
fireshots = pygame.sprite.Group()
iceshots = pygame.sprite.Group()
lightningshots = pygame.sprite.Group()
watershots = pygame.sprite.Group()
blocks = pygame.sprite.Group()
hardBlocks = pygame.sprite.Group()
enemies = pygame.sprite.Group()
backgrounds = pygame.sprite.Group()
players = pygame.sprite.Group()
all = pygame.sprite.OrderedUpdates()

Player.containers = (all, players)
Block.containers = (all, blocks)
Spikes.containers = (all, spikes, blocks)
HardBlock.containers = (all, hardBlocks, blocks)
BigBird.containers = (all, bigbirds)
DemonBunny.containers = (all, demonbunnies)
Enemy.containers = (all, enemies)
Background.containers = (all, blocks)
Shot.containers = (all, shots)
FireShot.containers = (all, fireshots)
IceShot.containers = (all, iceshots)
LightningShot.containers = (all, lightningshots)
WaterShot.containers = (all, watershots)
FireWarrior.containers = (all, firewarriors)

bg = Background("rsc/bg/Background.png", screensize)

def loadLevel(level):
    f = open(level+".lvl", 'r')
    lines = f.readlines()
    f.close()
    
    newlines = []
    
    for line in lines:
        newline = ""
        for c in line:
            if c != "\n":
                newline += c
        newlines += [newline]
        
    for line in newlines:
        print line
    
    for y, line in enumerate(newlines):
        for x, c in enumerate(line):
            if c == "g":
                HardBlock("rsc/blocks/Grass.png", 
                      [(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2], 
                      blocksize)
            elif c == "i":
                HardBlock("rsc/blocks/IceBlock.png", 
                      [(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2], 
                      blocksize)
            elif c == "h":
                HardBlock("rsc/blocks/Dirt.png", 
                      [(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2], 
                      blocksize)
            elif c == "w":
                Block("rsc/blocks/WaterBlock.png", 
                      [(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2], 
                      blocksize)
            elif c == "s":
                Spikes("rsc/blocks/Spikes.png", 
                      [(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2], 
                      blocksize)
            elif c == "a":
                HardBlock("rsc/blocks/Floatblock.png", 
                      [(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2], 
                      blocksize)
    f = open(level+".tng", 'r')
    lines = f.readlines()
    f.close()
    
    newlines = []
    
    for line in lines:
        newline = ""
        for c in line:
            if c != "\n":
                newline += c
        newlines += [newline]
        
    for line in newlines:
        print line
    
    for y, line in enumerate(newlines):
        for x, c in enumerate(line):
            if c == "@":
                player = Player([(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2],
                     playersize,
                     screensize)
            elif c == "4":
                Enemy("rsc/enemy/Satan.png", 
                      [(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2], 
                      playersize)
            elif c == "3":
                FireWarrior("rsc/enemy/FireWarrior.png", 
                      [(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2], 
                      playersize)
            elif c == "1":
                DemonBunny("rsc/enemy/DemonBunny.png", 
                      [(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2], 
                      playersize)
            elif c == "6":
                Enemy("rsc/enemy/Bill.png", 
                      [(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2], 
                      playersize)
            elif c == "5":
                Enemy("rsc/enemy/Cthulhu.png", 
                      [(x*blocksize[0])+blocksize[0]/8, (y*blocksize[1])+blocksize[1]/8], 
                      playersize)
            elif c == "2":
                BigBird("rsc/enemy/BigBird.png", 
                      [(x*blocksize[0])+blocksize[0]/2, (y*blocksize[1])+blocksize[1]/2], 
                      playersize)
    for each in all.sprites():
        each.fixLocation(player.offsetx, player.offsety)
     
    

levels = ["rsc/levels/level1",
          "rsc/levels/level2",
          "rsc/levels/level3"]
level = 0
loadLevel(levels[level])
player1 = players.sprites()[0]

while True:
    while player1.living:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    if level < len(levels)-1:
                        level += 1
                    else:
                        level = 0
                    for each in all.sprites():
                        each.kill()
                    bg = Background("rsc/bg/Background.png", screensize)
                    screen.blit(bg.image, bg.rect)
                    loadLevel(levels[level])
                    player1 = players.sprites()[0]

                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    player1.direction("right")
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    player1.direction("left")
                if event.key == pygame.K_s or event.key == pygame.K_DOWN:
                    player1.direction("down")
                if event.key == pygame.K_KP1 or event.key == pygame.K_1:
                    FireShot(player1.headingx, player1.rect.center, screensize)
                if event.key == pygame.K_KP2 or event.key == pygame.K_2:
                    WaterShot(player1.headingx, player1.rect.center, screensize)
                if event.key == pygame.K_KP3 or event.key == pygame.K_3:
                    IceShot(player1.headingx, player1.rect.center, screensize)
                if event.key == pygame.K_KP5 or event.key == pygame.K_5:
                    LightningShot(player1.headingx, player1.rect.center, screensize)
                if event.key == pygame.K_SPACE:
                    
                    player1.direction("jump")
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    player1.direction("stop right")
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    player1.direction("stop left")
                if event.key == pygame.K_w or event.key == pygame.K_UP:
                    player1.direction("stop up")
                if event.key == pygame.K_s or event.key == pygame.K_DOWN:
                    player1.direction("stop down")
                    
        playersHitBlocks = pygame.sprite.groupcollide(players, hardBlocks, False, False)
        playersHitSpikes = pygame.sprite.groupcollide(players, spikes, False, False)
        firewarriorsHitBlocks = pygame.sprite.groupcollide(firewarriors, hardBlocks, False, False)
        enemiesHitBlocks = pygame.sprite.groupcollide(enemies, hardBlocks, False, False)
        bigbirdsHitBlocks = pygame.sprite.groupcollide(bigbirds, hardBlocks, False, False)
        demonbunniesHitBlocks = pygame.sprite.groupcollide(demonbunnies, hardBlocks, False, False)
        playersHitFireWarriors = pygame.sprite.groupcollide(players, bigbirds, True, True)
        playersHitDemonBunnies = pygame.sprite.groupcollide(players, demonbunnies, True, True)
        playersHitBigBirds = pygame.sprite.groupcollide(players, firewarriors, True, True)
        watershotsHitFireWarriors = pygame.sprite.groupcollide(watershots, firewarriors, True, True)
        iceshotsHitBigBirds = pygame.sprite.groupcollide(iceshots, bigbirds, True, True)
        lightningshotsHitBigBirds = pygame.sprite.groupcollide(lightningshots, bigbirds, True, True)
        fireshotsHitDemonBunnies = pygame.sprite.groupcollide(fireshots, demonbunnies, True, True)
        
        for player in playersHitBlocks:
            for block in playersHitBlocks[player]:
                player.collideBlock(block)
                
        for player in playersHitSpikes:
            for spikes in playersHitSpikes[player]:
                player1.living = False
                bg = Background("rsc/StartScreen/EndScreen.png", screensize)

            
        for player in playersHitFireWarriors:
            for block in playersHitFireWarriors[player]:
                player1.living = False
                bg = Background("rsc/StartScreen/EndScreen.png", screensize)

        for player in playersHitBigBirds:
            for block in playersHitBigBirds[player]:
                player1.living = False
                bg = Background("rsc/StartScreen/EndScreen.png", screensize)
    
        for player in playersHitDemonBunnies:
            for block in playersHitDemonBunnies[player]:
                player1.living = False
                
        for firewarrior in firewarriorsHitBlocks:
            for block in firewarriorsHitBlocks[firewarrior]:
                firewarrior.collideBlock(block)
                
        for demonbunny in demonbunniesHitBlocks:
            for block in demonbunniesHitBlocks[demonbunny]:
                demonbunny.collideBlock(block)
                
        for bigbird in bigbirdsHitBlocks:
            for block in bigbirdsHitBlocks[bigbird]:
                bigbird.collideBlock(block)
                
        for enemy in enemiesHitBlocks:
            for block in enemiesHitBlocks[enemy]:
                enemy.collideBlock(block)
                
        
                    
        all.update(screensize,
                   player1.speedx, 
                   player1.speedy, 
                   player1.scrollingx, 
                   player1.scrollingy,
                   player1.realx,
                   player1.realy)
        
        dirty = all.draw(screen)
        pygame.display.update(dirty)
        pygame.display.flip()
        clock.tick(30)
        
        
    sys.exit()








